package com.climb.moive.controller;

import com.climb.moive.entity.Member;
import com.climb.moive.framework.mvc.BaseController;
import com.climb.moive.service.MemberService;
import com.climb.moive.service.MovieEvalService;
import com.climb.moive.service.MovieReviewService;
import com.climb.moive.service.impl.MemberServiceImpl;
import com.climb.moive.service.impl.MoiveEvalServiceImpl;
import com.climb.moive.service.impl.MovieReviewServiceImpl;
import com.climb.moive.util.CommmonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Administrator
 * 管理员-会员管理
 */
@WebServlet("/adminMember")
    public class AdminMemberController extends BaseController {
    MemberService service = new MemberServiceImpl();
    MovieEvalService movieEvalService=new MoiveEvalServiceImpl();
    MovieReviewService movieReviewService=new MovieReviewServiceImpl();
    /**跳转到会员管理界面*/
    @Override
    public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得所有会员信息

        List<Member> list = service.findAll();
        request.setAttribute("memberList", list);
        return "/view/admin/AdminMemberManagement.jsp";
    }
    /**修改会员信息-指向修改信息页面AdminMemberDetailModify.jsp*/
    public String findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取到member-id
         Integer id= Integer.parseInt( request.getParameter("id"));
         //根据这个id去找用户传回去
         Member member=service.findMemberById(id);
         if (member!=null){
             /*会员对象*/
             request.setAttribute("member",member);
             /*会员评价数evaluationNum*/
             request.setAttribute("evaluationNum",movieReviewService.countMovieEvaluationNum(id));
             /*会员文章数essayNum*/
             request.setAttribute("essayNum",movieEvalService.countMovieEvaluationNum(id));
             /*跳转至详情页面*/
             return "forward:/view/admin/AdminMemberDetailModify.jsp";
         }else {
             return "/view/admin/AdminMemberManagement.jsp";
         }

    }
    /**修改用户信息界面-修改用户信息*/
    public String modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Member member=new Member();
        CommmonUtils.parseBean(member,request.getParameterMap());

        /*获取查询这个对象的密码覆盖表单传过来的没有密码的对象*/
        Member oldMember=service.findMemberById(member.getId());
        member.setRegister_time(oldMember.getRegister_time());
        member.setPassword(oldMember.getPassword());
        /*更新成员对象*/
        service.update(member);
        System.out.println(member);
         /*重新搜索列表*/
        List<Member> members=service.findAll();
        request.setAttribute("memberList",members);
        /*重定位到列表*/
        return "/view/admin/AdminMemberManagement.jsp";
    }

    /*查看会员信息详情*/
    public String details(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.valueOf(request.getParameter("id"));
        /*获取个人呢的评论和文章数*/
        /*会员评价数evaluationNum*/
        request.setAttribute("evaluationNum",movieReviewService.countMovieEvaluationNum(id));
        /*会员文章数essayNum*/
        request.setAttribute("essayNum",movieEvalService.countMovieEvaluationNum(id));
        request.setAttribute("member",service.findMemberById(id));
        return "/view/admin/AdminMemberDetail.jsp";
    }
    /*删除会员*/
    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id=Integer.valueOf(request.getParameter("id"));
        /*先删除文章表和文件表的所有关联*/
        movieEvalService.delet(id);
        /*删除会员表的该会员*/
        movieReviewService.delet(id);
        service.delet(id);
        /*重新搜索列表*/
        List<Member> members=service.findAll();
        request.setAttribute("memberList",members);
        /*重定位到列表*/
        return "/view/admin/AdminMemberManagement.jsp";
    }
    /**跳转至会员查询*/
    public String doSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        return "forward:/view/admin/AdminSearchMember.jsp";
    }
    /*会员查询searchById-结果处理*/
    public String searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return details(request,response);
    }
    /**会员查询searchByName-结果处理*/
    public String searchByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输入名字
        String name=request.getParameter("name");
        List<Member> members=service.findMembersByName(name);
        request.setAttribute("memberList", members);
        /*跳转到用户列表*/
        return "/view/admin/AdminMemberManagement.jsp";
    }

}
